/*   1:    */ package org.intellij.plugins.relaxNG.compact.folding;
/*   2:    */ 
/*   3:    */

import com.intellij.codeInsight.folding.CodeFoldingSettings;
import com.intellij.lang.ASTNode;
import com.intellij.lang.folding.FoldingBuilder;
import com.intellij.lang.folding.FoldingDescriptor;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiWhiteSpace;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.TokenSet;
import org.intellij.plugins.relaxNG.compact.RncElementTypes;
import org.intellij.plugins.relaxNG.compact.RncTokenTypes;
import org.intellij.plugins.relaxNG.compact.psi.RncAnnotation;
import org.intellij.plugins.relaxNG.compact.psi.RncName;
import org.intellij.plugins.relaxNG.compact.psi.util.EscapeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */ 
/*  21:    */ public class RncFoldingBuilder
/*  22:    */   implements FoldingBuilder
/*  23:    */ {
/*  24:    */   @NotNull
/*  25:    */   public FoldingDescriptor[] buildFoldRegions(@NotNull ASTNode paramASTNode, @NotNull Document paramDocument)
/*  26:    */   {
/*  27: 47 */     if (paramASTNode == null) {
/*  28: 47 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/intellij/plugins/relaxNG/compact/folding/RncFoldingBuilder", "buildFoldRegions" }));
/*  29:    */     }
/*  30: 47 */     if (paramDocument == null) {
/*  31: 47 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "1", "org/intellij/plugins/relaxNG/compact/folding/RncFoldingBuilder", "buildFoldRegions" }));
/*  32:    */     }
/*  33: 47 */     ArrayList localArrayList = new ArrayList();
/*  34: 48 */     a(paramASTNode, paramDocument, localArrayList); FoldingDescriptor[] 
/*  35:    */     
/*  36: 50 */       tmp121_118 = (localArrayList.size() > 0 ? (FoldingDescriptor[])localArrayList.toArray(new FoldingDescriptor[localArrayList.size()]) : FoldingDescriptor.EMPTY);
/*  37: 50 */     if (tmp121_118 == null) {
/*  38: 50 */       throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/plugins/relaxNG/compact/folding/RncFoldingBuilder", "buildFoldRegions" }));
/*  39:    */     }
/*  40: 50 */     return tmp121_118;
/*  41:    */   }
/*  42:    */   
/*  43:    */   public String getPlaceholderText(@NotNull ASTNode paramASTNode)
/*  44:    */   {
/*  45: 56 */     if (paramASTNode == null) {
/*  46: 56 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/intellij/plugins/relaxNG/compact/folding/RncFoldingBuilder", "getPlaceholderText" }));
/*  47:    */     }
/*  48: 56 */     IElementType localIElementType = paramASTNode.getElementType();
/*  49: 57 */     if (localIElementType == RncTokenTypes.LBRACE) {
/*  50: 58 */       return "{ ... }";
/*  51:    */     }
/*  52: 59 */     if (b(localIElementType)) {
/*  53: 60 */       return "# ...";
/*  54:    */     }
/*  55: 61 */     if (a(localIElementType))
/*  56:    */     {
/*  57: 62 */       RncName localRncName = ((RncAnnotation)paramASTNode.getPsi()).getNameElement();
/*  58: 63 */       if (localRncName != null)
/*  59:    */       {
/*  60: 64 */         ASTNode localASTNode = localRncName.getNode();
/*  61: 65 */         assert (localASTNode != null);
/*  62: 66 */         return EscapeUtil.unescapeText(localASTNode) + " [ ... ]";
/*  63:    */       }
/*  64: 68 */       return "[ ... ]";
/*  65:    */     }
/*  66: 70 */     return "...";
/*  67:    */   }
/*  68:    */   
/*  69:    */   private static boolean a(IElementType paramIElementType)
/*  70:    */   {
/*  71: 75 */     return (RncElementTypes.ANNOTATION == paramIElementType) || (RncElementTypes.ANNOTATION_ELEMENT == paramIElementType) || (RncElementTypes.FORWARD_ANNOTATION == paramIElementType);
/*  72:    */   }
/*  73:    */   
/*  74:    */   private static boolean b(IElementType paramIElementType)
/*  75:    */   {
/*  76: 79 */     return (RncTokenTypes.COMMENTS.contains(paramIElementType)) || (RncTokenTypes.DOC_TOKENS.contains(paramIElementType));
/*  77:    */   }
/*  78:    */   
/*  79:    */   public boolean isCollapsedByDefault(@NotNull ASTNode paramASTNode)
/*  80:    */   {
/*  81: 83 */     if (paramASTNode == null) {
/*  82: 83 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/intellij/plugins/relaxNG/compact/folding/RncFoldingBuilder", "isCollapsedByDefault" }));
/*  83:    */     }
/*  84: 83 */     return (b(paramASTNode.getElementType())) && (CodeFoldingSettings.getInstance().COLLAPSE_DOC_COMMENTS);
/*  85:    */   }
/*  86:    */   
/*  87:    */   private static void a(@Nullable ASTNode paramASTNode, Document paramDocument, ArrayList<FoldingDescriptor> paramArrayList)
/*  88:    */   {
/*  89: 87 */     if (paramASTNode == null) {
/*  90: 88 */       return;
/*  91:    */     }
/*  92: 91 */     ASTNode[] arrayOfASTNode = paramASTNode.getChildren(RncTokenTypes.BRACES);
/*  93: 92 */     if (arrayOfASTNode.length == 2)
/*  94:    */     {
/*  95: 93 */       ASTNode localASTNode1 = arrayOfASTNode[0];
/*  96: 94 */       ASTNode localASTNode2 = arrayOfASTNode[1];
/*  97: 95 */       if (a(localASTNode1, localASTNode2, paramDocument))
/*  98:    */       {
/*  99: 96 */         TextRange localTextRange = new TextRange(localASTNode1.getStartOffset(), localASTNode2.getTextRange().getEndOffset());
/* 100: 97 */         paramArrayList.add(new FoldingDescriptor(localASTNode1, localTextRange));
/* 101:    */       }
/* 102:    */     }
/* 103: 99 */     else if ((a(paramASTNode.getElementType())) && 
/* 104:100 */       (b(paramASTNode.getFirstChildNode(), paramASTNode.getLastChildNode(), paramDocument)))
/* 105:    */     {
/* 106:101 */       paramArrayList.add(new FoldingDescriptor(paramASTNode, paramASTNode.getTextRange()));
/* 107:    */     }
/* 108:105 */     paramASTNode = paramASTNode.getFirstChildNode();
/* 109:106 */     while (paramASTNode != null)
/* 110:    */     {
/* 111:107 */       paramASTNode = a(paramASTNode, RncTokenTypes.DOC_TOKENS, paramArrayList, paramDocument);
/* 112:    */       
/* 113:109 */       paramASTNode = a(paramASTNode, RncTokenTypes.COMMENTS, paramArrayList, paramDocument);
/* 114:    */       
/* 115:111 */       a(paramASTNode, paramDocument, paramArrayList);
/* 116:113 */       if (paramASTNode != null) {
/* 117:114 */         paramASTNode = paramASTNode.getTreeNext();
/* 118:    */       }
/* 119:    */     }
/* 120:    */   }
/* 121:    */   
/* 122:    */   @Nullable
/* 123:    */   private static ASTNode a(@Nullable ASTNode paramASTNode, TokenSet paramTokenSet, ArrayList<FoldingDescriptor> paramArrayList, Document paramDocument)
/* 124:    */   {
/* 125:121 */     if ((paramASTNode != null) && (paramTokenSet.contains(paramASTNode.getElementType())))
/* 126:    */     {
/* 127:122 */       ASTNode localASTNode1 = paramASTNode;
/* 128:123 */       ASTNode localASTNode2 = localASTNode1;
/* 129:    */       
/* 130:125 */       paramASTNode = paramASTNode.getTreeNext();
/* 131:126 */       if (paramASTNode != null) {
/* 132:    */         do
/* 133:    */         {
/* 134:128 */           localASTNode2 = paramASTNode;
/* 135:129 */           paramASTNode = paramASTNode.getTreeNext();
/* 136:130 */         } while ((paramASTNode != null) && (paramTokenSet.contains(paramASTNode.getElementType())));
/* 137:    */       }
/* 138:132 */       if (localASTNode2 != localASTNode1)
/* 139:    */       {
/* 140:133 */         while ((localASTNode2.getPsi() instanceof PsiWhiteSpace)) {
/* 141:134 */           localASTNode2 = localASTNode2.getTreePrev();
/* 142:    */         }
/* 143:136 */         if (b(localASTNode1, localASTNode2, paramDocument)) {
/* 144:137 */           paramArrayList.add(new FoldingDescriptor(localASTNode1, new TextRange(localASTNode1.getStartOffset(), localASTNode2.getTextRange().getEndOffset())));
/* 145:    */         }
/* 146:    */       }
/* 147:    */     }
/* 148:141 */     return paramASTNode;
/* 149:    */   }
/* 150:    */   
/* 151:    */   private static boolean a(ASTNode paramASTNode1, ASTNode paramASTNode2, Document paramDocument)
/* 152:    */   {
/* 153:145 */     if (paramASTNode1.getElementType() != RncTokenTypes.LBRACE) {
/* 154:146 */       return false;
/* 155:    */     }
/* 156:147 */     if (paramASTNode2.getElementType() != RncTokenTypes.RBRACE) {
/* 157:148 */       return false;
/* 158:    */     }
/* 159:150 */     return b(paramASTNode1, paramASTNode2, paramDocument);
/* 160:    */   }
/* 161:    */   
/* 162:    */   private static boolean b(ASTNode paramASTNode1, ASTNode paramASTNode2, Document paramDocument)
/* 163:    */   {
/* 164:155 */     return paramDocument.getLineNumber(paramASTNode1.getStartOffset()) != paramDocument.getLineNumber(paramASTNode2.getStartOffset());
/* 165:    */   }
/* 166:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.intellij.plugins.relaxNG.compact.folding.RncFoldingBuilder

 * JD-Core Version:    0.7.0.1

 */